package com.blog.wu.myblog.controller;

import com.blog.wu.myblog.base.BaseController;
import com.blog.wu.myblog.entity.Permission;
import com.blog.wu.myblog.entity.Role;
import com.blog.wu.myblog.service.PermissionService;
import com.blog.wu.myblog.service.RoleService;
import com.blog.wu.myblog.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-11-06 11:37 AM
 * @Discription 系统设置controller
 */
@Controller
@RequestMapping("back/sys")
public class SysController extends BaseController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "menu/index", method = RequestMethod.GET)
    public String menuIndex() {

        return "back/sys/sys_menu_List";
    }

    @RequestMapping(value = "role/index", method = RequestMethod.GET)
    public String roleIndex(Role role, Model model, @ModelAttribute("info") String info) {
        List<Role> roleList = roleService.findList(new Role());
        model.addAttribute("roleList", roleList);

        return "back/sys/sys_role_List";
    }

    @RequestMapping(value = "role/loadUpdate", method = RequestMethod.GET)
    public String loadUpdate(Role role, Model model, @ModelAttribute("info") String info) {
        List<String> permissionIds = permissionService.findPermissionRole(role);
        role = roleService.get(role);
        model.addAttribute("role", role);
        model.addAttribute("permissionIds", permissionIds);

        return "back/sys/sys_role_update";
    }

    @RequestMapping(value = "role/update", method = RequestMethod.POST)
    public String roleUpdate(Role role, Model model, RedirectAttributes attributes) {
        List<Permission> listPage = role.getPermissions();
        List<Permission> permissionList = new ArrayList<>();
        for (Permission permission : listPage) {

            if (permission.isRootNode()) {
                continue;
            }
            permissionList.add(permission);
        }
        role.setPermissions(permissionList);
        boolean flag = roleService.updateRole(role);
        if (flag) {
            attributes.addAttribute("info", "更新角色成功");
        } else {
            attributes.addAttribute("info", "更新角色失败");
        }
        attributes.addAttribute("id", role.getId());
        return "redirect:/back/sys/role/loadUpdate";
    }

    @RequestMapping(value = "role/delete", method = RequestMethod.GET)
    public String roleDelete(Role role, Model model, RedirectAttributes attributes) {
        int count = roleService.countUserRole(role);
        if (count > 0) {
            attributes.addAttribute("info", "还有用户和该角色绑定，请先解绑用户！");
            return "redirect:/back/sys/role/index";
        }
        boolean flag = roleService.deleteRole(role);
        if (flag) {
            attributes.addAttribute("info", "删除角色成功");
        } else {
            attributes.addAttribute("info", "删除角色失败");
        }
        return "redirect:/back/sys/role/index";
    }

    @RequestMapping(value = "role/add", method = RequestMethod.POST)
    public String roleAdd(Role role, Model model, RedirectAttributes attributes) {
        List<Permission> listPage = role.getPermissions();
        List<Permission> permissionList = new ArrayList<>();
        for (Permission permission : listPage) {
            String parentId = "0";
            if (permission.getParentId().equals(parentId)) {
                continue;
            }
            permissionList.add(permission);
        }
        role.setPermissions(permissionList);
        boolean flag = roleService.saveRole(role);
        if (flag) {
            attributes.addAttribute("info", "新增角色成功");
        } else {
            attributes.addAttribute("info", "新增角色失败");
        }

        return "redirect:/back/sys/role/index";
    }


    @RequestMapping(value = "menu/update", method = RequestMethod.POST)
    public String updateMenu(Role role) {

        //删除菜单
        List<Permission> listDb = permissionService.findList(new Permission());
        List<Permission> listPage = role.getPermissions();
        if (listPage.size() < listDb.size()) {
            List<Permission> reduct = ListUtil.listCompare(Permission.class.getName(), "id", listPage, listDb);
            for (Permission p : reduct) {
                permissionService.delete(p);
                permissionService.deleteRolePermission(p);
            }
        }

        //更新菜单
        for (Permission p : role.getPermissions()) {
            p.setTarget("mainframe");
            //如果是新增的菜单
            if (permissionService.get(p) == null) {
                permissionService.add(p);
            }
            permissionService.update(p);
        }

        return "back/sys/sys_menu_List";
    }

    @RequestMapping(value = "menu/show", method = RequestMethod.POST)
    @ResponseBody
    public List<Permission> showMenu() {
        List<Permission> permissionList = permissionService.findList(new Permission());
        return permissionList;
    }

}
